{
 "cells": [
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "# 多重派发"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "在这个notebook中我们将探索Julia的重要特性之一：**多重派发**。\n",
    "\n",
    "多重派发让软件更*通用（generic）*，更*快*！\n",
    "\n",
    "#### 从熟悉的开始讲\n",
    "\n",
    "从我们已经接触过的知识来理解多重派发。\n",
    "\n",
    "我们可以声明一个Julia函数而不用给出传入变量的任何信息："
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "f(x) = x^2"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "Julia自会判断哪些传入变量是合理的，哪些又是不合理的："
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "f(10)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "f([1, 2, 3])"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "#### 指定传入变量的类型\n",
    "\n",
    "但是，我们*也可以*明确指定传入变量的类型。\n",
    "\n",
    "比如，我们写一个只接受字符串类型传入变量的函数`foo`。"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "foo(x::String, y::String) = println(\"My inputs x and y are both strings!\")"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "可以看到，要限制`x`和`y`的类型为`String`，只要在形参名后面加上两个冒号和关键字`String`就行了。\n",
    "\n",
    "现在`foo`只有在传入变量为`String`类型才正常工作。"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "foo(\"hello\", \"hi!\")"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "foo(3, 4)"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "要让`foo`函数接受整型（`Int`）的传入变量，只要在声明`foo`函数时在形参后面加上`::Int`。"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "foo(x::Int, y::Int) = println(\"My inputs x and y are both integers!\")"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "foo(3, 4)"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "现在函数`foo`能接受整型变量了！但是注意，函数`foo`仍然能接受字符串变量！"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "foo(\"hello\", \"hi!\")"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "这就涉及到多重派发的核心了。当我们声明\n",
    "\n",
    "```julia\n",
    "foo(x::Int, y::Int) = println(\"My inputs x and y are both integers!\")\n",
    "```\n",
    "我们并没有重写或覆盖\n",
    "```julia\n",
    "foo(y::String, y::String)\n",
    "```\n",
    "\n",
    "而是给叫做`foo`的***通用函数（generic function）***添加了一个***新方法（method）***。\n",
    "\n",
    "一个***通用函数***是一个表示特定操作的抽象概念。\n",
    "\n",
    "举个例子，通用函数`+`代表了加法这个概念\n",
    "\n",
    "一个***方法***是一个***通用函数***接受*特定参数类型*的一个具体实现。\n",
    "\n",
    "举个例子，`+`有不同的方法以接受浮点数、整型、矩阵等等。\n",
    "\n",
    "我们可以通过函数`methods`来查看函数`foo`有多少种方法。"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "methods(foo)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "methods(+)"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "所以，我们现在可以在调用`foo`的时候传入整型或字符串了。当你调用`foo`时传入特定组合的参数时，Julia将根据传入参数的类型派发对应的方法。*这*就是多重派发。\n",
    "\n",
    "多重派发让我们的代码更加通用更加快。代码更加通用更灵活是因为我们是在写代码描述抽象操作例如加法和乘法，而不是描述具体的实现。同时，因为Julia可以为相关类型调用高效的方法，代码可以运行得很快。\n",
    "\n",
    "想查看我们调用一个通用函数时派发的是哪个方法，我们可以用宏@which："
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "@which foo(3, 4)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "@which 3.0 + 3.0"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "考虑到一个特别写给浮点数的方法派发给了`3.0 + 3.0`，生成的LLVM代码及其简练："
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "@code_llvm 3.0 + 3.0"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {
    "collapsed": true
   },
   "source": [
    "要注意的是Julia在我们写一般的函数定义时也很快，因为具体的、定制的方法最终是在后台调用的。\n",
    "\n",
    "举个例子，我们声明加法函数`myadd`时没有类型注释——"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "myadd(x, y) = x + y"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "虽然我们没有限制`x`和`y`的类型，但是`myadd(3.0, 3.0)`生成的LLVM代码和`3.0 + 3.0`差不多"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "@code_llvm myadd(3.0, 3.0)"
   ]
  }
 ],
 "metadata": {
  "anaconda-cloud": {},
  "kernelspec": {
   "display_name": "Julia 1.0.5",
   "language": "julia",
   "name": "julia-1.0"
  },
  "language": "Julia",
  "language_info": {
   "file_extension": ".jl",
   "mimetype": "application/julia",
   "name": "julia",
   "version": "1.0.5"
  },
  "toc": {
   "nav_menu": {
    "height": "119px",
    "width": "251px"
   },
   "navigate_menu": true,
   "number_sections": true,
   "sideBar": true,
   "threshold": "2",
   "toc_cell": false,
   "toc_section_display": "block",
   "toc_window_display": false
  }
 },
 "nbformat": 4,
 "nbformat_minor": 1
}
